import axios from "axios";
// 引入进度条插件
import nprogress from 'nprogress';
import 'nprogress/nprogress.css';
import { getToken } from "./token";
const request = axios.create({
  baseURL: '/app',
  timeout: 50000
});

// 添加请求拦截器
request.interceptors.request.use(config => {
  // 进度条开启
  nprogress.start();
  // 获取 token
  let token = getToken();
  token && (config.headers.token = token);
  return config;
});

// 添加响应拦截器
request.interceptors.response.use((res) => {
  // 进度条关闭
  nprogress.done();
  return res.data;
}, (error) => {
  return new Error(error.message);
});

export default request;